package com.dejiao.controller;


import com.dejiao.entity.ResultEntity;
import com.dejiao.entity.po.UserInfo;
import com.dejiao.entity.vo.LoginSuccessVo;
import com.dejiao.entity.vo.LoginVo;
import com.dejiao.myenum.ResponseEnum;
import com.dejiao.service.UserService;
import com.dejiao.utils.JWT;
import com.dejiao.utils.PasswordUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriComponentsBuilder;

/**
 * Created by Germmy on 2018/6/30.
 */
@RestController
@RequestMapping("/user")
public class UserController {

    //添加一个日志器
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;


    //注册
    //TODO


    //登录
    /**
     * @api {get} http://xxx/xiaomifeng/pmc/pwdquery/{webSiteName}  根据webSiteName查询密码实体
     * @apiName GetPasswordEntity
     * @apiGroup PasswordEntity
     * @apiParam {String} webSiteName 密码实体的唯一编号
     * @apiSuccess {String} webSiteName 密码实体的唯一编号
     * @apiSuccess {String} webSiteName  网站名称
     * @apiSuccess {String} userName  用户名
     * @apiSuccess {String} password  密码
     * @apiSuccessExample Success-Response:
     * 0000000 OKxxxx
     * {
     * "errCode": "0000000",
     * "errorMessage": "success",
     * "data": {
     * "webSiteName": "jd",
     * "userName": "wandejun1012",
     * "password": "123456",
     * "peId": "abcdefg"
     * }
     * }
     * @apiError 1000001 该密码实体找不到
     * @apiError 1000002 xxxx
     * @apiError 9999999 服务器开小差了
     * @apiErrorExample Error-Response:
     * {
     * "errCode": "1000001",
     * "errorMessage": "该密码实体找不到",
     * "data": null
     * }
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<ResultEntity<LoginSuccessVo>> doLogin(@RequestBody LoginVo loginVo, UriComponentsBuilder ucBuilder) throws  Exception {
        //输出日志文件
        logger.info("user login start"+loginVo);//这个要用AOP改写
        ResultEntity<LoginSuccessVo> resultEntity = new ResultEntity<>();
        loginVo.setPassword(PasswordUtil.encryPasswordWithSalt(loginVo.getPassword()));//先要将password加密
        UserInfo userInfo = userService.selectByUserNameAndPassword(loginVo);
        if(null != userInfo){
            //存在此用户，登录成功，返回jwt
            String jwt= JWT.sign(userInfo.getMobile(),30L * 24L * 3600L * 1000L);//有效期1个月
            LoginSuccessVo loginSuccessVo=new LoginSuccessVo();
            loginSuccessVo.setJwt(jwt);
            //设置re
            resultEntity.setErrCode(ResponseEnum.SUCCESS.getErrorCode());
            resultEntity.setErrorMessage("登录成功");
            resultEntity.setData(loginSuccessVo);
        }else{
            //登录失败
            //设置re
            resultEntity.setErrCode(ResponseEnum.USER_PASSWORD_NOT_CORRECT.getErrorCode());
            resultEntity.setErrorMessage(ResponseEnum.USER_PASSWORD_NOT_CORRECT.getErrorMessage());
            resultEntity.setData(null);
        }
        logger.info("user login end");
        return new ResponseEntity<>(resultEntity, HttpStatus.OK);
    }

}
